<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrappe">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="接单日期" style="height: 32px;">
              <a-range-picker
                :allowClear="false"
                v-model="invitation"
                style="width: 100%"
                @change="invitationChange"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item
              style="height: 32px;"
              label="项目名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol">
              <select-project-input v-model="queryParam.projectId" placeholder="请选择项目" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="采购人员">
              <select-user-by-dep placeholder="请选择采购人员" v-model="queryParam.proposer" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="业务版块">
              <j-dict-select-tag type="list" v-model="queryParam.businessSegment" dictCode="business_segment" placeholder="请选择业务板块" />
            </a-form-item>
          </a-col>
          <!-- <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="项目类型">
              <j-dict-select-tag  type="list" v-model="queryParam.projectGenre" dictCode="purchase_project_type" placeholder="请选择项目类型" />
            </a-form-item>
          </a-col> -->
          <!-- <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="中标总金额">
              <a-input-number :min="0" :precision="0" style="width: 45%" placeholder="请输入中标总金额最小值" class="query-group-cust" v-model="queryParam.biddingAmountBegin"></a-input-number>
              <span class="query-group-split-cust"></span>
              <a-input-number :min="queryParam.biddingAmountBegin" :precision="0" style="width: 45%" placeholder="请输入中标总金额最大值" class="query-group-cust" v-model="queryParam.biddingAmountEnd"></a-input-number>
            </a-form-item>
          </a-col> -->
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="费用所属团队">
              <select-team-input placeholder="请选择费用所属团队"  v-model="queryParam.teamId" :tdlx="3" :teamType="0" :multi="true" />
            </a-form-item>
          </a-col>
          <!-- <template v-if="toggleSearchStatus"></template> -->
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="onClearSelected();searchQuery();" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <!-- <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a> -->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" icon="download" @click="handleExportXls('年度项目汇总登记表')" :disabled="isDisabledAuth('erpPurchaseCalibrationStanding:export')">导出</a-button>
      <!-- 高级查询区域 -->
      <!--<j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>-->
      <a-popover title="自定义列" trigger="click" placement="leftBottom">
        <template slot="content">
          <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
            <a-row>
              <template v-for="(item,index) in defColumns">
                <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                  <a-col :span="12" :key="index"><a-checkbox :value="item.dataIndex">{{ item.title }}</a-checkbox></a-col>
                </template>
              </template>
            </a-row>
          </a-checkbox-group>
        </template>
        <a style="float: right;"><a-icon type="setting" :style="iconStyle"/></a>
      </a-popover>
    </div>
    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>
      <a-table
        ref="table"
        size="middle"
        :components="draggableComponent"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="false"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        :loading="loading"
        class="j-table-force-nowrap"
        :customRow="customRow"
        @change="tableChangePages">
        <a slot="projectId" slot-scope="text, record" @click="onDetail(record.projectId)">{{ record.projectId_dictText }}</a>
      </a-table>
    </div>
    <div style="padding: 10px; display: flex; justify-content: flex-end;" v-if="rowSpanPagination.totalPage">
      <Pagination :total-page="rowSpanPagination.totalPage" :current-page="rowSpanPagination.pageNo" :visiblePages="rowSpanPagination.pageSize" :total="rowSpanPagination.total" @page-change="handlePageChange" />
    </div>
    <!-- 项目详情开始 -->
    <erp-project-report-modal  ref="modalForm"></erp-project-report-modal>
    <!-- 项目详情结束 -->
  </a-card>
</template>
<script>
  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import {DisabledAuthFilterMixin} from '@/mixins/DisabledAuthFilterMixin'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
  import SelectProjectInput from '@/views/scy/components/SelectProjectInput'
  import moment from 'moment'
  import {getAction, downFile} from '@/api/manage'
  import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
  import ErpProjectReportModal from '@/views/erp/report/project/modules/ErpProjectReportModal'
  import Pagination from '@/views/erp/report/audit/Pagination'
  export default {
    name: 'ErpPurchaseCalibrationStandingList',
    mixins:[JeecgListMixin, mixinDevice, DisabledAuthFilterMixin],
    components: {
      SelectTeamInput,
      SelectUserByDep,
      SelectProjectInput,
      ErpProjectReportModal,
      Pagination
    },
    data () {
      return {
        description: '年度项目汇总登记表管理页面',
        rowSpanPagination: {
          pageNo: 1,
          pageSize: 5,
          totalPage: 0,
          total: 0
        },
        //表头
        columns:[],
        //列设置
        settingColumns:[],
        // 列定义
        defColumns: [
          // {
          //   title: '序号',
          //   dataIndex: '',
          //   key:'rowIndex',
          //   width:60,
          //   align:"center",
          //   customRender:function (t,r,index) {
          //     return parseInt(index)+1;
          //   }
          // },
          {
            title:'项目名称',
            align:"center",
            dataIndex: 'projectId_dictText',
            scopedSlots: { customRender: 'projectId_dictText' },
            width: 200,
            ellipsis: true,
            customRender: (value, row, index) => {
              // 使用a标签跳转
              const obj = {
                children: <a onClick={() => this.onDetail(row.projectId)}>{value}</a>,
                attrs: {},
                
              };
              const obj1 = this.customRow(row, index);
              obj.attrs.rowSpan = obj1.rowspan
              return obj;
            }
          },
          {
            title:'项目承接人',
            align:"center",
            width: 100,
            dataIndex: 'undertaker_dictText',
            ellipsis: true,
            customRender: (value, row, index) => {
              const obj = {
                children: value,
                attrs: {}
              };
              const obj1 = this.customRow(row, index);
              obj.attrs.rowSpan = obj1.rowspan
              return obj;
            }
          },
          {
            title:'招标采购发起人',
            align:"center",
            width: 100,
            dataIndex: 'invitationProposer_dictText',
            ellipsis: true,
            customRender: (value, row, index) => {
              const obj = {
                children: value,
                attrs: {}
              };
              const obj1 = this.customBiddingRow(row, index);
              obj.attrs.rowSpan = obj1.rowspan
              return obj;
            }
          },
          {
            title:'发起部门/团队',
            align:"center",
            dataIndex: 'departId_dictText',
            width: 200,
            ellipsis: true,
            customRender: (value, row, index) => {
              const obj = {
                children: value,
                attrs: {}
              };
              const obj1 = this.customBiddingRow(row, index);
              obj.attrs.rowSpan = obj1.rowspan
              return obj;
            }
          },
          {
            title:'费用所属团队',
            align:"center",
            dataIndex: 'teamId_dictText',
            width: 200,
            ellipsis: true,
            customRender: (value, row, index) => {
              const obj = {
                children: value,
                attrs: {}
              };
              const obj1 = this.customBiddingRow(row, index);
              obj.attrs.rowSpan = obj1.rowspan
              return obj;
            }
          },
          {
            title:'业务板块',
            align:"center",
            dataIndex: 'businessSegment_dictText',
            width: 100,
            ellipsis: true,
            customRender: (value, row, index) => {
              const obj = {
                children: value,
                attrs: {}
              };
              const obj1 = this.customBiddingRow(row, index);
              obj.attrs.rowSpan = obj1.rowspan
              return obj;
            }
          },
          {
            title:'项目类型',
            align:"center",
            width: 150,
            dataIndex: 'description',
            ellipsis: true,
            customRender: (value, row, index) => {
              const obj = {
                children: value,
                attrs: {}
              };
              const obj1 = this.customBiddingRow(row, index);
              obj.attrs.rowSpan = obj1.rowspan
              return obj;
            }
          },
          {
            title:'接单日期',
            align:"center",
            width: 100,
            dataIndex: 'invitationUpdateTime',
            ellipsis: true,
            customRender: (value, row, index) => {
              const obj = {
                children: value,
                attrs: {}
              };
              const obj1 = this.customBiddingRow(row, index);
              obj.attrs.rowSpan = obj1.rowspan
              return obj;
            }
          },
          {
            title:'采购总预算(元)',
            align:"center",
            dataIndex: 'budget',
            width: 100,
            ellipsis: true,
            customRender: (value, row, index) => {
              const obj = {
                children: value,
                attrs: {}
              };
              const obj1 = this.customBiddingRow(row, index);
              obj.attrs.rowSpan = obj1.rowspan
              return obj;
            }
          },
          {
            title:'预计节约金额(元)',
            align:"center",
            dataIndex: 'economyMoney',
            width: 100,
            ellipsis: true,
            customRender: (value, row, index) => {
              const obj = {
                children: value,
                attrs: {}
              };
              const obj1 = this.customBiddingRow(row, index);
              obj.attrs.rowSpan = obj1.rowspan
              return obj;
            }
          },
          {
            title:'采购人员',
            align:"center",
            width: 100,
            ellipsis: true,
            dataIndex: 'calibrationProposer_dictText',
            ellipsis: true,
            customRender: (value, row, index) => {
              const obj = {
                children: value,
                attrs: {}
              };
              const obj1 = this.customCalibrationRow(row, index);
              obj.attrs.rowSpan = obj1.rowspan
              return obj;
            }
          },
          
          {
            title:'定标进度',
            align:"center",
            dataIndex: 'bpmStatus',
            width: 100,
            ellipsis: true,
            customRender: (value, row, index) => {
              const obj = {
                children: value,
                attrs: {}
              };
              const obj1 = this.customCalibrationRow(row, index);
              obj.attrs.rowSpan = obj1.rowspan
              return obj;
            }
          },
          {
            title:'定标日期',
            align:"center",
            dataIndex: 'calibrationUpdateTime',
            width: 100,
            ellipsis: true,
            // customRender:function (text) {
            //   return !text?"":(text.length>10?text.substr(0,10):text)
            // }
            customRender: (value, row, index) => {
              const obj = {
                children: value,
                attrs: {}
              };
              const obj1 = this.customCalibrationRow(row, index);
              obj.attrs.rowSpan = obj1.rowspan
              return obj;
            }
          },
          {
            title:'定标备注',
            align:"center",
            dataIndex: 'remark',
            width: 150,
            ellipsis: true,
            customRender: (value, row, index) => {
              const obj = {
                children: value,
                attrs: {}
              };
              const obj1 = this.customCalibrationRow(row, index);
              obj.attrs.rowSpan = obj1.rowspan
              return obj;
            }
          },
          {
            title:'中标单位名称',
            align:"center",
            dataIndex: 'biddingUnit_dictText',
            width: 100,
            ellipsis: true
          },
          {
            title:'中标金额(元)',
            align:"center",
            width: 100,
            dataIndex: 'biddingAmount',
            ellipsis: true
          },
          {
            title:'发票类型',
            align:"center",
            dataIndex: 'invoiceCategory_dictText',
            width: 100,
            ellipsis: true
          },
          {
            title:'税点(%)',
            align:"center",
            dataIndex: 'taxRate',
            width: 100,
            ellipsis: true
          },
          {
            title:'付款方式',
            align:"center",
            dataIndex: 'payType',
            width: 150,
            ellipsis: true
          }
        ],
        url: {
          list: "/api/erp/purchase/statistical/erpPurchaseCalibrationStanding/list",
          exportXlsUrl: "/api/erp/purchase/statistical/erpPurchaseCalibrationStanding/exportXls",
          queryById: '/api/erp/project/report/erpProjectReport/queryById'
        },
        dictOptions:{},
        superFieldList:[],
        invitation: [],
        invitationStartTime: null,
        invitationEndTime: null,
        visibleDetail: false,
      }
    },
    created() {
      this.getSuperFieldList();
    },
    methods: {
      moment,
      tableChangePages(pagination, filters, sorter){
        this.rowSpanPagination = pagination
        this.rowSpanPagination.pageNo = pagination.current
        this.loadData()
      },
      handlePageChange(pageNo) {
        this.rowSpanPagination.pageNo = pageNo
        this.loadData()
      },
      loadData(arg) {
        if (!this.url.list) {
          this.$message.error('请设置url.list属性!')
          return
        }
        //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.rowSpanPagination.pageNo = 1;
      }
        if(this.invitation.length == 0) {
          this.initMonthDate()
        }
        var params = this.getQueryParams() // 查询条件
        this.loading = true
        this.dataSource = []
        getAction(this.url.list, {...params, pageNo: this.rowSpanPagination.pageNo, pageSize: this.rowSpanPagination.pageSize}).then(res => {
          if (res.success) {
            this.dataSource = res.result.records || res.result
            this.rowSpanPagination.totalPage = res.result? res.result.pages : 0
            this.rowSpanPagination.total = res.result? res.result.total : 0
          }
          if (res.code === 510) {
            this.$message.warning(res.message)
          }
          this.loading = false
        })
      },

      // 合并同类项
      // 一个项目对应多个招标
      customRow(record, index) {
        const rowSpan = this.getRowSpan(record.id, index, 'id', record.projectSize)
        return {
          rowspan: rowSpan
        }
      },
      // 一个招标对应多个定标
      customBiddingRow(record, index) {
        const rowSpan = this.getRowSpan(record.invitationId, index, 'invitationId', record.invitationSize)
        return {
          rowspan: rowSpan
        }
      },
      // 一个定标对应多个中标
      customCalibrationRow(record, index) {
        const rowSpan = this.getRowSpan(record.calibrationId, index, 'calibrationId',  record.calibrationSize)
        return {
          rowspan: rowSpan
        }
      },
      getRowSpan(value, index, column, size) {
        if (size === 1 || size === null || size === undefined) {
          return 1
        } else {
          if (index === 0) {
            return size
          }
          // 对比下一个是否存在且相等，如果相等则合并单元格。
          for (let i = index - 1; i >= 0; i--) {
              if (this.dataSource[i][column] === value) {
              size = 0
              break
            }
          }
        }
        return size
      },
      //初始化合同签订日期查询条件
      initMonthDate() {
        let date = new Date();//当前时间
        let year= date.getFullYear();//获取当前年
        let month = date.getMonth()  + 1;//获取当前月
        let day = date.getDate();//获取当前日
        month = (month > 9) ? month : ("0" + month);//设置 月份格式
        day = (day < 10) ? ("0" + day) : day;//设置 日格式
        let today = year + "-" + month + "-" + day;//当前日期

        let invitationStartTime = year +'-01-01';
        let invitationEndTime = today;
        this.invitation = [moment(invitationStartTime, 'YYYY-MM-DD'), moment(invitationEndTime, 'YYYY-MM-DD')]
        this.$set(this.queryParam, 'invitationStartTime', invitationStartTime + ' 00:00:00')
        this.$set(this.queryParam, 'invitationEndTime', invitationEndTime + ' 23:59:59')
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'projectId',text:'项目名称',dictCode:''})
        fieldList.push({type:'string',value:'proposer',text:'采购人员',dictCode:''})
        fieldList.push({type:'string',value:'departId',text:'发起部门/团队',dictCode:''})
        fieldList.push({type:'string',value:'invitationProposer',text:'发起人',dictCode:''})
        fieldList.push({type:'string',value:'teamId',text:'项目所属团队',dictCode:''})
        fieldList.push({type:'string',value:'undertaker',text:'项目承接人'})
        fieldList.push({type:'string',value:'businessSegment',text:'业务板块',dictCode:''})
        // fieldList.push({type:'string',value:'projectGenre',text:'项目类型',dictCode:''})
        fieldList.push({type:'date',value:'invitationUpdateTime',text:'接单日期',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'budget',text:'预算价(元)',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'biddingAmount',text:'中标金额(元)',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'economyMoney',text:'预计节约金额(元)',dictCode:''})
        fieldList.push({type:'string',value:'bpmStatus',text:'定标进度',dictCode:''})
        fieldList.push({type:'date',value:'calibrationUpdateTime',text:'定标日期',dictCode:''})
        fieldList.push({type:'string',value:'successfulInformation',text:'中标信息',dictCode:''})
        fieldList.push({type:'string',value:'remark',text:'备注',dictCode:''})
        this.superFieldList = fieldList
      },
      invitationChange(date, dateString) {
        this.$set(this.queryParam, 'invitationStartTime', dateString[0] + ' 00:00:00')
        this.$set(this.queryParam, 'invitationEndTime', dateString[1] + ' 23:59:59')
      },
      searchReset() {
        this.invitation = []
        this.queryParam = {}
        this.loadData(1)
      },
      onDetail(id) {
        const that = this
        this.$nextTick(() => {
          getAction(this.url.queryById, { id }).then((res) => {
            if (res.success) {
              const model = res.result
              that.$refs.modalForm.getDetail(model);
              that.$refs.modalForm.title = '项目一览表';
            }
          })
        })
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
